// sub_jigouguanli/jigouedit/jigouedit.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orizid: "",
    form: {
      jigouName: "",
      jigouShort: "",
      jigouCode: "",
      jigouType: "",
      jigouLevel: "分支行",
      beBranch: "--",
      jigouLocation: "",
      jigouAddress: "",
      jigouLinkman: "",
      isBankjoint: true,
      fenpei: '0',
    },
    isYinjian: false,
    firstId: '',
    // 机构类型
    jigouTypeIshow: false,
    jigouTypeArr: ["城市商业银行", "村镇银行", "股份制商业银行", "国有大型银行", "金融租赁公司", "开发性金融机构", "农村商业银行", "企业集团财务公司", "外资银行", "消费金融公司", "信托公司", "政策性银行"],
    // 机构级别
    jigouLevelIsshow: false,
    // jigouLevelArr: ["总行", "分行", "分支行"],
    jigouLevelArr: ["一级行", "二级行", "分支行"],
    jigouLevelIndex: 3,
    // 所属分支行
    beBranchIshow: false,
    beBranchArr: ["--"],
    beBranchId: "",
    beBranchIndex: "",
    // 所属区域
    jigouLocationIsshow: false,
    jigouLocationArr: ["姑苏区", "工业园区", "高新区", "相城区", "吴中区", "吴江区", "昆山市", "太仓市", "常熟市", "张家港市"],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.isFiradmin()
    // console.log(options);
    if (options.orizid) {
      this.setData({
        orizid: options.orizid,
        firstId: options.firstId || ''
      })
      const res = await getApp().globalData.request(
        "/corpVisit/getOrganizationInfo", {
        organizationId: options.orizid
      },
        "POST"
      )
      console.log(res, 'res');
      this.setData({
        ['form.jigouName']: res.data.name,
        ['form.jigouShort']: res.data.shortName,
        ['form.jigouCode']: res.data.code,
        ['form.jigouType']: res.data.type,
        ['form.jigouLevel']: this.data.jigouLevelArr[res.data.grade - 1],
        jigouLevelIndex: res.data.grade,
        // ['form.beBranch']: res.data.name,
        ['form.jigouLocation']: res.data.region,
        beBranchId: res.data.belongTo,
        ['form.jigouAddress']: res.data.address,
        ['form.jigouLinkman']: res.data.contactsPhone,
        ['form.isBankjoint']: res.data.isParticipate === '1' ? true : false,
        ['form.fenpei']: res.data.assignmentLevel + '',
      })
      console.log(this.data.form, 'form')
    }
    let params = {}
    if (this.data.firstId) {
      params.organizationId = this.data.firstId
    }
    console.log(params, 'params')
    // 获取所属分支行列表
    getApp().globalData.request(
      "/corpVisit/getOrganizationList", params,
      "POST"
    ).then(res => {
      console.log(res, '获取所属分支行列表');
      res.data.unshift({
        name: "--",
        organizationId: "0"
      })
      this.setData({
        beBranchArr: res.data
      })
      if (this.data.beBranchId !== "") {
        this.data.beBranchArr.forEach((item, index) => {
          if (item.organizationId == this.data.beBranchId) {
            this.setData({
              ['form.beBranch']: item.name,
              beBranchIndex: index * 1
            })
          }
        })
      }
    })
  },
  // 判断是否是银监
  isFiradmin() {
    let arr = wx.getStorageSync("userInfo").corpRole;
    if (arr.indexOf("cbrc") !== -1) {
      this.setData({
        isYinjian: true
      })
    } else {
      this.setData({
        isYinjian: false
      })
    }
  },
  onChangeMore1(e) {
    // console.log(e, 'e')
    const value = e.currentTarget.dataset.value
    const key = e.currentTarget.dataset.key
    this.data.form[key] = value
    this.setData({
      form: this.data.form
    })
  },
  // methed
  setjigouName(e) {
    this.setData({
      ["form.jigouName"]: e.detail.value
    })
  },
  setjigoushort(e) {
    this.setData({
      ["form.jigouShort"]: e.detail.value
    })
  },
  setjigouCode(e) {
    this.setData({
      ["form.jigouCode"]: e.detail.value
    })
  },
  setjigouAddress(e) {
    this.setData({
      ["form.jigouAddress"]: e.detail.value
    })
  },
  setjigouLinkman(e) {
    this.setData({
      ["form.jigouLinkman"]: e.detail.value
    })
  },
  // 机构类型
  jigouTypeShow() {
    this.setData({
      jigouTypeIshow: true
    })
  },
  jigouTypeConfirm(e) {
    this.setData({
      ["form.jigouType"]: e.detail.value,
      jigouTypeIshow: false,
    });
  },
  jigouTypeCancel() {
    this.setData({
      jigouTypeIshow: false,
    });
  },
  // 机构级别
  jigouLevelShow() {
    this.setData({
      jigouLevelIsshow: true,
    });
  },
  jigouLevelConfirm(e) {
    console.log(e);
    this.setData({
      ["form.jigouLevel"]: e.detail.value,
      jigouLevelIsshow: false,
      jigouLevelIndex: e.detail.index * 1 + 1
    });
  },
  jigouLevelCancel() {
    this.setData({
      jigouLevelIsshow: false,
    });
  },
  //所属分支行
  beBrancheShow() {
    if (this.data.form.jigouLevel == '一级行' || this.data.form.jigouLevel == '二级行') return
    this.setData({
      beBranchIshow: true,
    });
  },
  beBranchConfirm(e) {
    console.log(e);
    this.setData({
      ["form.beBranch"]: e.detail.value.name,
      beBranchId: e.detail.value.organizationId,
      beBranchIshow: false,
    });
  },
  beBranchCancel() {
    this.setData({
      beBranchIshow: false,
    });
  },
  // 所属区域
  jigouLocationShow() {
    this.setData({
      jigouLocationShow: true,
    });
  },
  jigouLocationConfirm(e) {
    this.setData({
      ["form.jigouLocation"]: e.detail.value,
      jigouLocationShow: false,
    });
  },
  jigouLocationCancel() {
    this.setData({
      jigouLocationShow: false,
    });
  },
  // 是否银企对接
  isBankjointChange(e) {
    console.log(e);
    this.setData({
      ['form.isBankjoint']: e.detail
    });
  },
  // 点击删除
  deleteJigou() {
    getApp().globalData.request(
      "/corpVisit/deleteOrganization", {
      organizationId: this.data.orizid
    },
      "POST"
    ).then(res => {
      if (res.code == 200) {
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        prevPage.setData({ // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
          refresh: true,
          list: [],
          pageNum: 1,
          isManage: false,
          ischecked: false,
        })
        wx.navigateBack({
          delta: 1 // 返回上一级页面。
        })
      }

    })
  },
  // 点击添加
  editJigoiu() {
    for (const key in this.data.form) {
      // console.log(key);
      // console.log(this.data.form[key]);
      if (key !== 'jigouLinkman' && key !== "isBankjoint") {
        if (!this.data.form[key]) {
          wx.showToast({
            title: '请先完善信息',
            icon: "none"
          })
          return
        } else {

        }
      }
    }

    let params = {
      "address": this.data.form.jigouAddress,
      "belongTo": this.data.beBranchId == '--' ? '' : this.data.beBranchId,
      "code": this.data.form.jigouCode,
      "contactsPhone": this.data.form.jigouLinkman,
      "organizationId": this.data.orizid,
      "grade": this.data.jigouLevelIndex,
      "isParticipate": this.data.form.isBankjoint ? '1' : '0',
      "name": this.data.form.jigouName,
      "region": this.data.form.jigouLocation,
      "shortName": this.data.form.jigouShort,
      "type": this.data.form.jigouType,
      "assignmentLevel": this.data.form.jigouLevel == '一级行' ? this.data.form.fenpei : '',
    }
    console.log(params, "修改提交的参数");
    getApp().globalData.request(
      "/corpVisit/updateOrganizationInfo", params,
      "POST"
    ).then(res => {
      console.log(res);
      let pages = getCurrentPages();
      let prevPage = pages[pages.length - 2];
      prevPage.setData({ // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
        refresh: true
      })
      wx.navigateBack({
        delta: 1 // 返回上一级页面。
      })
    })
    if (Object.hasOwnProperty.call(object, key)) {
      const element = object[key];

    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})